package main

import (
	"bufio"
	"encoding/json"
	"errors"
	"fmt"
	"io"
	"os"
)

var (
	firstName,LastName,s string
	i int
	f float32
	input = "56.12/5212/Go"
	format = "%f/%d%s"
)

type User struct {
	Name string
	Age  int
	Sex  string
}
func main() {


  	//insts:=make([]*balance.Instance,10)
  	//fmt.Println(insts)
	//t:=test1.Cat{}
	//t.Hh()
	//fmt.Println(insts)
	////
	//for i:=0; i<16; i++ {
	//	host := fmt.Sprintf("192.168.%d.%d",rand.Intn(255),rand.Intn(255))
	//	port := rand.Intn(10066)
	//	one := balance.NewInstance(host, port)
	//	insts = append(insts,one)
	//}
	//
	//balancer := &balance.RandomBalance{}
  //
	//for true {
	//	inst,err := balancer.DoBalance(insts)
	//	if err != nil {
	//		fmt.Println("Do balance err:",err)
	//		continue
	//	}
	//	fmt.Println(inst)
	//	time.Sleep(time.Second)
	//}
	//a:=balance.NewInstance("127.0.0.1",80)
	//
	//fmt.Println(a.GetHost())

	//文件
	//fmt.Println("Please enter your full name")
	//fmt.Scanln(&firstName,&LastName)
	//fmt.Printf("Hi %s %s !\n",firstName,LastName)
	//fmt.Sscanf(input,format,&f,&i,&s)
	//fmt.Println("form the string we read :",f,i,s)

	//格式化
	//var a string = "王亚伦 27 男"
	//var user User
	////fmt.Sscanf(a,"%s %d %s", &user.Name,&user.Age,&user.sex)
	////fmt.Println(a)
	//arr:=strings.Fields(a)
	//user.Name=arr[0]
	//user.Age, _ = strconv.Atoi(arr[1])
	//user.Sex = arr[2]
	//fmt.Println(user)

	//文件读取

	//inputReader := bufio.NewReader(os.Stdin)
	//fmt.Println("请输入一些字")
	//input, _ = inputReader.ReadString('\n')
	//fmt.Printf("这个输入是:%s\n",input)
	//终端读写--从终端读取一行字符串，统计英文，数字，空格，以及其他字符
	//font := bufio.NewReader(os.Stdin)
	//fmt.Println("请输入一些字")
	//input, _ = font.ReadString('\n')
	//var rNumber = regexp.MustCompile(`\d`)
	//var rString = regexp.MustCompile(`[a-zA-z]`)
	//var nullString = regexp.MustCompile(" ")
	//fmt.Println(len(rNumber.FindAllStringSubmatch(input,-1)))
	//fmt.Println(len(rString.FindAllStringSubmatch(input,-1)))
	//fmt.Println(len(nullString.FindAllStringSubmatch(input,-1)))
	////写入文件
	//inputFile, err := os.OpenFile("./1.txt",os.O_CREATE|os.O_APPEND|os.O_RDWR, 0666)
	//if err != nil {
	//	fmt.Println("你的文件出错了")
	//	return
	//}
	//ss, _ :=inputFile.WriteString("hhhhhdasdsadas你的哥哥是我")
	//fmt.Println(ss)

	//读整个文件 拷贝
	//inputFile := "./1.txt"
	//outputFile := "./1copay.txt"
	//buf,err:=ioutil.ReadFile(inputFile)
	//if err != nil {
	//	fmt.Println("文件错误",err)
	//	return
	//}
	//fmt.Printf("%s\n",string(buf))
	//err = ioutil.WriteFile(outputFile,buf,777)
	//if err != nil {
	//	fmt.Println(err.Error())
	//}

	//文件写入示例
	//outputFile, outputError := os.OpenFile("1.txt",os.O_CREATE|os.O_APPEND|os.O_RDWR, 777)
	//if outputError  != nil{
	//	fmt.Println("文件错误")
	//	return
	//}
	//defer outputFile.Close()
	//
	//outputString:="hello world ! \n"
	//for i:=0;i<10;i++{
	//	outputFile.WriteString(outputString)
	//}
	//文件拷贝
	//FileCopy("1.txt","2.txt")
	//fmt.Println("拷贝完成")
	//命令行参数
	//var test bool
	//var str string
	//var count int
	//
	//flag.BoolVar(&test,"b",false,"print on newline")
	//flag.StringVar(&str,"s","","print on newline")
	//flag.IntVar(&count,"c",1001,"print one newline")
	//flag.Parse()
	//fmt.Println(test)
	//fmt.Println(str)
	//fmt.Println(count)
	//flag.Parse()
	//if flag.NArg() == 0 {
	//	cat(bufio.NewReader(os.Stdin))
	//}
	//for i:=0; i<flag.NArg();i++ {
	//	f1,err := os.Open(flag.Arg(i))
	//	if err != nil {
	//		fmt.Fprintf(os.Stderr,"%s:error reading from %s; %s\n",os.Args[0],flag.Arg(i),err.Error())
	//	}
	//	continue
	//}
	//cat(bufio.NewReader(f1))
	//12 带缓冲区的终端读写
	//fmt.Fprintf(os.Stdout,"%s\n","hello world!-unbuffered")
	//buf:=bufio.NewReader(os.Stdout)
	//fmt.Fprintf(buf,"%s\n","hello world! - buffered")
	//buf.Flush()
	//json 字符串
	var e map[string]int = make(map[string]int)
 	var b map[string]int = make(map[string]int)
 	b["11"]=1
 	b["cc"]=2
	fmt.Println(b)
	c,_:= json.Marshal(b)
	fmt.Println(c)
	json.Unmarshal(c,&e)
	fmt.Println(e)
	//错误处理

	fmt.Printf("error: %v",errNotFound)
	fmt.Println(1)


}

type PathError struct {
	Op string
	Path string
	err string
}

func (e *PathError)Error() string {
return  e.Op + "" + e.Path + ":"+ e.Err.Error()
}
type error interface {
	Error() string
}
var errNotFound error = errors.New("Not found error")
func cat(r *bufio.Reader)  {
	for  {
		buf,err := r.ReadBytes('\n')
		if err == io.EOF {
				break
		}
		fmt.Fprintf(os.Stdout,"%s",buf)
		return
	}
}
func FileCopy(f1 string,f2 string) (wr int64,err error ) {
	 f1_open, err := os.Open(f1) //直接打开
	if err != nil{
		return
	}

	f2_open, err2 := os.OpenFile(f2,os.O_APPEND|os.O_CREATE|os.O_RDWR,0777) //直接打开
	defer f1_open.Close()
	if err2 !=nil  {
		return
	}
	defer  f2_open.Close()
	return io.Copy(f2_open,f1_open)

}